﻿using System;
using System.Data;
using System.Drawing;
using System.Net;
using System.IO;

namespace JSClass
{
	/// <summary>
    /// JSUnionImage의 요약 설명입니다.
	/// </summary>
	public class JSImage
	{
		public String src = "";
		public int width = 0;
		public int height = 0;
        public int top = -10;
        public int left = -10;
        public float opacity = 1;
		public Bitmap bitmap = null;
        
        public JSImage()
        {
        }
        
        public JSImage(String _src, int _width, int _height, int _top, int _left, float _opacity)
        {
            this.src = _src;
            this.width = _width;
            this.height = _height;
            this.top = _top;
            this.left = _left;
            this.opacity = _opacity;

            WebClient wc = new WebClient();
            Stream stImage1 = wc.OpenRead(this.src);
            this.bitmap = new Bitmap(stImage1);
        }

        public void setWidth(int _width) { this.width = _width; }
        public void setHeight(int _height) { this.height = _height; }
        public void setTop(int _top) { this.top = _top; }
        public void setLeft(int _left) { this.left = _left; }
        public void setOpacity(float _opacity) { this.opacity = _opacity; }
        
        public void setSrc(String _src)
        {
            this.src = _src;
            
            WebClient wc = new WebClient();
            Stream stImage1 = wc.OpenRead(this.src);
            this.bitmap = new Bitmap(stImage1);
        }
	}
	
    /// <summary>
    /// JSUnionImage의 요약 설명입니다.
	/// </summary>
	public class JSUnionImage
	{
        private Vector images;
        private Bitmap unionImage;
        private string imgFileName = "unionImage.png";
        private string imgFilePath = @"C:\Inetpub\wwwroot\LampGIS2011\UnionImage\";
        private int imgWidth = 1024;
        private int imgHeight = 768;
        
        public JSUnionImage() {
            this.images = new Vector();
        }
        
        public void SetFileName(string _fileName) {this.imgFileName = _fileName;}
        public void SetFilePath(string _filePath) { this.imgFilePath = _filePath; }
        public void setImgWidth(int _width) { this.imgWidth = _width; }
        public void setImgHeight(int _height) { this.imgHeight = _height; }

        public void SetSize(int _width , int _height)
        {
            this.imgWidth = _width;
            this.imgHeight = _height;
        }
        public void AddImage(JSImage _image)
        {
            this.images.Add(_image);
        }
        
        public void AddImage(String _src, int _width, int _height, int _top, int _left)
        {
            this.images.Add(new JSImage(_src, _width, _height, _top, _left, 1));
        }
        public void AddImage(String _src, int _width, int _height, int _top, int _left, string _opacity)
        {
            this.images.Add(new JSImage(_src, _width, _height, _top, _left, float.Parse(_opacity)));
        }
        public void AddImage(String _src, string _width, string _height, string _top, string _left, string _opacity)
        {
            this.images.Add(new JSImage(_src, int.Parse(_width), int.Parse(_height), int.Parse(_top), int.Parse(_left), float.Parse(_opacity)));
        }

        public System.Drawing.Bitmap UnionImages()
        {
            this.unionImage = new Bitmap(this.imgWidth, this.imgHeight);
            System.Drawing.Graphics graphics = Graphics.FromImage(this.unionImage);

            for (int i = 0; i < this.images.Count; i++)
            {
                JSImage image = (JSImage)this.images.elementAt(i);
                float[][] ptsArray ={
                        new float[] {1, 0, 0, 0, 0},
                        new float[] {0, 1, 0, 0, 0},
                        new float[] {0, 0, 1, 0, 0},
                        new float[] {0, 0, 0, image.opacity, 0},
                        new float[] {0, 0, 0, 0, 1}};
                System.Drawing.Imaging.ColorMatrix clrMatrix = new System.Drawing.Imaging.ColorMatrix(ptsArray);
                System.Drawing.Imaging.ImageAttributes imgAttributes = new System.Drawing.Imaging.ImageAttributes();
                imgAttributes.SetColorMatrix(clrMatrix,
                    System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap);

                System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(image.left, image.top, image.width, image.height);

                graphics.DrawImage(image.bitmap,
                        rectangle,
                        0, 0, image.width, image.height,
                        System.Drawing.GraphicsUnit.Pixel, imgAttributes);
            }

            /*
            Point[] apt=new Point[5];
            for(int i=0; i < 6 ; i++)
            {
                double dAngle=(i*0.8-0.5)*Math.PI;
                apt[i]=new Point(
                    (int)(200*(0.50+0.48*Math.Cos(dAngle))+20),
                    (int)(200*(0.50+0.48*Math.Sin(dAngle))+30)
                );
            }
            Pen p=new Pen(Color.Blue);
            graphics.DrawEllipse(p, 20, 30, 200, 200);
            graphics.DrawPolygon(p, apt);
            */

            graphics.Dispose();

            return this.unionImage;
        }

        public void SaveImage()
        {
            if (this.unionImage != null)
            {
                this.unionImage.Save(this.imgFilePath + this.imgFileName);
            }
        }
    }
}